﻿@using NewLife;
@{
    var fmt = Model as String;
    var formatStr = !fmt.IsNullOrEmpty() ? fmt : "yyyy-MM-dd";
    //var name = fmt == "yyyy-MM-dd" ? "日期" : "时间";
    var p = ViewBag.Page as Pager;

    var dtStart = p["dtStart"].ToDateTime();
    var dtEnd = p["dtEnd"].ToDateTime();
    var step = (Int32)(dtEnd - dtStart).TotalDays + 1;

    var isEmpty = dtStart.Year < 2000 && dtEnd.Year < 2000;
}
<div class="form-group">
    @*<label for="dtStart" class="control-label">@name：</label>*@
    <div class="input-group">
        @if (formatStr == "yyyy-MM-dd" && (dtStart.Year > 2000 || dtEnd.Year > 2000))
        {
            var url = p.GetBaseUrl(true, true, true, new[] { "dtStart", "dtEnd" });
            if (dtStart.Year > 2000 && dtEnd.Year > 2000) url.UrlParam("dtStart", dtStart.AddDays(-step).ToString("yyyy-MM-dd"));
            if (dtStart.Year > 2000 && dtEnd.Year > 2000) url.UrlParam("dtEnd", dtEnd.AddDays(-step).ToString("yyyy-MM-dd"));
            <span class="input-group-addon"><a href="?@Html.Raw(url)" title="前一段"><i class="fa fa-calendar"></i></a></span>
        }
        else
        {
            <span class="input-group-addon"><i class="fa fa-calendar" title="点击使用今日"></i></span>
        }
        <input name="dtStart" id="dtStart" value="@p["dtStart"]" dateformat="@formatStr" class="form-control form_datetime" autocomplete="off" />
    </div>
    @if (formatStr == "yyyy-MM-dd" && (dtStart.Year > 2000 || dtEnd.Year > 2000))
    {
        var url = p.GetBaseUrl(true, true, true, new[] { "dtStart", "dtEnd" });
        if (dtStart.Year > 2000 && dtEnd.Year > 2000) url.UrlParam("dtStart", dtStart.AddDays(step).ToString("yyyy-MM-dd"));
        if (dtStart.Year > 2000 && dtEnd.Year > 2000) url.UrlParam("dtEnd", dtEnd.AddDays(step).ToString("yyyy-MM-dd"));
        <label for="dtEnd" class="control-label"><a href="?@Html.Raw(url)" title="后一段">~</a></label>
    }
    else
    {
        <label for="dtEnd" class="control-label">~</label>
    }
    <div class="input-group">
        @*<span class="input-group-addon"><i class="fa fa-calendar"></i></span>*@
        <input name="dtEnd" id="dtEnd" value="@p["dtEnd"]" dateformat="@formatStr" class="form-control form_datetime" autocomplete="off" />
    </div>
</div>
@if (isEmpty)
{
    <script>
        $(function () {
            $('.fa-calendar').on('click', function () {
                // 获取当前日期
                var now = new Date();
                var yyyy = now.getFullYear();
                var mm = String(now.getMonth() + 1).padStart(2, '0');
                var dd = String(now.getDate()).padStart(2, '0');
                var today = yyyy + '-' + mm + '-' + dd;

                // 填充到输入框
                $('#dtStart').val(today);
                $('#dtEnd').val(today);
            });
        });
    </script>
}